<template>
  <div class="dataset-container">
    <div class="header-container">
      <div class="title">数据配置</div>
    </div>
    <div class="search-section">
      <search-bar @search="handleSearch" />
    </div>
    <el-table 
      v-loading="loading"
      :data="tableData" 
      style="width: 100%" 
      :border="false"
      :cell-style="{ border: 'none' }"
      :header-cell-style="{ border: 'none' }"
      class="no-border-table">
      <el-table-column prop="id" label="ID" width="80" align="center" header-align="center" />
      <el-table-column prop="apiId" label="接口ID" width="120" align="center" header-align="center" />
      <el-table-column prop="description" label="数据描述" align="center" header-align="center" />
      <el-table-column prop="data" label="数据" align="center" header-align="center" />
      <el-table-column prop="createDate" label="创建日期" width="180" align="center" header-align="center" />
      <el-table-column label="操作" width="150" align="center" header-align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <template slot="empty">
        <empty-state />
      </template>
    </el-table>

    <!-- 编辑对话框 -->
    <el-dialog
      title="编辑数据集"
      :visible.sync="dialogVisible"
      width="calc(50% - 100px)"
      :before-close="handleClose"
    >
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="接口ID">
          <el-input v-model="editForm.apiId" placeholder="请输入接口ID"></el-input>
        </el-form-item>
        <el-form-item label="数据描述">
          <el-input type="textarea" v-model="editForm.description" placeholder="请输入数据描述" :rows="3"></el-input>
        </el-form-item>
        <el-form-item label="数据">
          <el-input type="textarea" v-model="editForm.data" placeholder="请输入数据" :rows="3"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="handleSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import SearchBar from './SearchBar.vue'
import EmptyState from './EmptyState.vue'

export default {
  name: 'Dataset',
  components: {
    SearchBar,
    EmptyState
  },
  data() {
    return {
      tableData: [
        {
          id: 1,
          apiId: 'API001',
          description: '用户信息数据配置',
          data: '{"username":"admin","role":"administrator","permissions":["read","write","delete"]}',
          createDate: '2023-10-15 09:30:45'
        },
        {
          id: 2,
          apiId: 'API002',
          description: '产品目录配置',
          data: '{"categories":["电子产品","家居用品","办公用品"],"featured":true,"maxItems":50}',
          createDate: '2023-11-02 14:22:10'
        },
        {
          id: 3,
          apiId: 'API003',
          description: '系统通知模板',
          data: '{"title":"{{title}}","content":"{{content}}","footer":"感谢使用我们的服务","showLogo":true}',
          createDate: '2023-12-05 11:15:33'
        },
        {
          id: 4,
          apiId: 'API004',
          description: '数据统计配置',
          data: '{"period":"monthly","metrics":["visits","sales","conversions"],"exportFormat":"excel"}',
          createDate: '2024-01-18 16:40:27'
        },
        {
          id: 5,
          apiId: 'API005',
          description: '支付网关参数',
          data: '{"gateway":"alipay","merchantId":"MER12345","callbackUrl":"https://example.com/callback","testMode":false}',
          createDate: '2024-02-22 10:05:59'
        }
      ],
      originalTableData: [], // 用于存储原始数据
      dialogVisible: false, // 控制对话框显示
      loading: false, // 添加loading状态
      editForm: {
        id: null,
        apiId: '',
        description: '',
        data: ''
      }
    }
  },
  created() {
    // 保存原始数据
    this.originalTableData = [...this.tableData]
  },
  methods: {
    handleSearch(searchData) {
      // 重置数据
      let filteredData = [...this.originalTableData]

      // 处理第一个搜索条件
      if (searchData.first.condition && searchData.first.value) {
        filteredData = filteredData.filter(item => {
          const value = String(item[searchData.first.condition]).toLowerCase()
          return value.includes(searchData.first.value.toLowerCase())
        })
      }

      // 处理第二个搜索条件
      if (searchData.second.condition && searchData.second.value) {
        filteredData = filteredData.filter(item => {
          const value = String(item[searchData.second.condition]).toLowerCase()
          return value.includes(searchData.second.value.toLowerCase())
        })
      }

      // 更新表格数据
      this.tableData = filteredData
    },
    handleEdit(row) {
      // 打开编辑对话框并填充数据
      this.editForm = { ...row }
      this.dialogVisible = true
    },
    handleClose(done) {
      // 关闭对话框前的处理
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    handleSave() {
      // 保存编辑的数据
      const index = this.tableData.findIndex(item => item.id === this.editForm.id)
      if (index !== -1) {
        this.tableData[index] = { ...this.editForm }
        this.originalTableData = [...this.tableData]
        this.dialogVisible = false
        this.$message({
          message: '保存成功',
          type: 'success'
        })
      }
    },
    handleDelete(row) {
      this.$confirm('确认删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('删除', row)
        // 这里添加删除逻辑
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
.dataset-container {
  padding: 0;
  width: 100%;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  margin-top: 13px;
  padding: 0 30px;
  flex-shrink: 0;
}

.title {
  font-weight: bold;
  margin-left: 0;
  font-size: 24px;
}

.search-section {
  margin-top: 30px;
  margin-bottom: 20px;
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 0 20px;
}

.no-border-table::before,
.no-border-table::after {
  display: none;
}

.no-border-table .el-table__header-wrapper th,
.no-border-table .el-table__body-wrapper td {
  border: none !important;
  text-align: center !important;
}

.no-border-table .el-table__header-wrapper th {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 500;
}

.no-border-table .el-table__body-wrapper td {
  padding: 12px 0;
}

.el-button + .el-button {
  margin-left: 8px;
}

/* 对话框样式 */
.dialog-footer {
  text-align: right;
}

.el-dialog {
  border-radius: 8px;
}

.el-dialog__header {
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.el-dialog__body {
  padding: 30px 20px;
}

.el-dialog__footer {
  padding: 20px;
  border-top: 1px solid #eee;
}
</style>
